package org.other.testPk;

/**
 * @Author yu
 * @Date 2022/7/8
 */
public class Demo3 {

    public static void main(String[] args) {

        int[] ints = {1, 5, 2, 9, 8, 0, 6};

        int[] sortArray = sortArray(ints);

        System.out.println(search(sortArray));
    }

    public static int search(int[] arr) {
        return arr[arr.length / 2];
    }

    public static int[] sortArray(int[] nums) {

        if (nums.length == 0 || nums.length == 1) {
            return nums;
        }

        quickSort(0, nums.length - 1, nums);

        return nums;
    }

    public static void quickSort(int left, int right, int[] arr) {
        if (left >= right) {
            return;
        }

        int l = left;
        int r = right;
        int flag = left;
        int temp = 0;

        while (left < right) {

            while (left < right && arr[flag] <= arr[right]) {
                right--;
            }

            if (arr[flag] > arr[right] && flag < right) {
                temp = arr[flag];
                arr[flag] = arr[right];
                arr[right] = temp;
                flag = right;
            }

            while (left < right && arr[flag] >= arr[left]) {
                left++;
            }
            if (arr[flag] < arr[left] && flag > left) {
                temp = arr[flag];
                arr[flag] = arr[left];
                arr[left] = temp;
                flag = left;
            }

        }

        quickSort(l, left - 1, arr);
        quickSort(left + 1, r, arr);
    }

}
